package com.tomorrowbbs.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;

public class StringTools {

    /**
     * 判断是否为空
     * @param str
     * @return
     */
    public static Boolean isEmpty(String str){
        if (str==null || "".equals(str.trim()) || "null".equals(str)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 生成随机数
     */
    public static final String getRandomString(Integer count){
        return RandomStringUtils.random(count,true,true);
    }
    public static final String getRandomNumber(Integer count){
        return RandomStringUtils.random(count,false,true);
    }

    //加密
    public static String encodeMd5(String sourceStr){
        return StringTools.isEmpty(sourceStr)?null: DigestUtils.md5Hex(sourceStr);
    }

    //文件后缀
    public static String getFileSuffix(String fileName){
        if(StringTools.isEmpty(fileName)){
            return null;
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }
    public static String getFileName(String fileName){
        return fileName.substring(0,fileName.lastIndexOf("."));
    }

    //内容转义
    public static String escapeHtml(String content){
        if(StringTools.isEmpty(content)){
            return content;
        }
        content = content.replace("&", "&amp;");
        content = content.replace("<", "&lt;");
        content = content.replace(">", "&gt;");
        content = content.replace("\"", "&quot;");
        content = content.replace("'", "&#39;");
        content = content.replace(" ","&nbsp");
        content =content.replace("\n","<br>");
        return content;
    }
}
